<?php

namespace App\Http\Controllers;

use App\Constants\ResponseCodeConstant;
use App\Http\Requests\MenuCreateRequest;
use App\Services\AdminMenuService;
use App\Traits\TreeTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 后台菜单管理
 * Class AdminUserController
 * @package App\Http\Controllers
 */
class AdminMenuController extends BaseController
{
    use TreeTrait;
    /**
     * 创建后台菜单
     * @param MenuCreateRequest $request
     * @return JsonResponse
     */
    public function create(MenuCreateRequest $request): JsonResponse
    {
        $parentId = $request->post('parent_id',0);
        $title = $request->post('title');
        $name = $request->post('name');
        $icon = $request->post('icon');
        $level = $request->post('level',0);
        $data = [
            'parent_id' => $parentId,
            'title' => $title,
            'name' => $name,
            'icon' => $icon,
            'level' => $level,
            'sort' => 0,
            'hidden' => 0
        ];

        $result = AdminMenuService::getInstance()->create($data);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::CREATE_FAILED,'菜单创建失败');
    }

    /**
     * 修改菜单信息
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function update(Request $request)
    {
        $id = $request->route('id');
        $data = [];
        if ($request->has('parent_id')) {
            $data['parent_id'] = $request->post('parent_id');
        }
        if ($request->has('title')) {
            $data['title'] = $request->post('title');
        }
        if ($request->has('name')) {
            $data['name'] = $request->post('name');
        }
        if ($request->has('icon')) {
            $data['icon'] = $request->post('icon');
        }
        if ($request->has('level')) {
            $data['level'] = $request->post('level');
        }
        if ($request->has('sort')) {
            $data['sort'] = $request->post('sort');
        }
        $result = false;
        if ($data) {
            $result = AdminMenuService::getInstance()->updateById($data,$id);
        }
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::UPDATE_FAILED,'菜单信息修改失败');
    }

    /**
     * 查询菜单详情
     * @param Request $request
     * @return JsonResponse
     */
    public function getById(Request $request): JsonResponse
    {
        $id = $request->route('id');
        $result = AdminMenuService::getInstance()->getById($id);
        if ($result) {
            return $this->success($result);
        }
        return $this->fail(ResponseCodeConstant::SEARCH_FAILED,'菜单信息查询失败');
    }

    /**
     * 删除菜单信息
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function delete(Request $request)
    {
        $id = $request->route('id');
        $result = AdminMenuService::getInstance()->deleteById($id);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::DELETE_FAILED,'删除菜单信息');
    }

    /**
     * 分页获取菜单信息
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $parentId = $request->route('parent_id',0);
        $pageSize = $request->get('pageSize',15);
        $pageNum = $request->get('pageNum',1);
        $data = AdminMenuService::getInstance()->list($parentId,$pageSize,$pageNum);
        $result = $this->successPagniate($data);
        return $this->success($result);
    }

    /**
     * 树形结构返回所有菜单列表
     * @return mixed
     */
    public function treeList()
    {
        $list = AdminMenuService::getInstance()->listAll();
        $result = [];
        if ($list) {
            $result = $this->arr2tree($list, 'id', 'parent_id', $son = 'sub');
        }
        return $this->success($result);
    }

    /**
     * 修改菜单显示状态
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function updateHidden(Request $request)
    {
        $id = $request->route('id');
        $hidden = $request->post('hidden');
        $data = [
            'hidden' => $hidden
        ];
        $result = AdminMenuService::getInstance()->updateById($data,$id);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::UPDATE_FAILED,'菜单信息修改失败');
    }
}
